package com.sugar.real.estate.module.business.controller.admin.infocls;

import com.sugar.real.estate.framework.apilog.core.annotation.ApiAccessLog;
import com.sugar.real.estate.framework.common.pojo.CommonResult;
import com.sugar.real.estate.framework.common.pojo.PageParam;
import com.sugar.real.estate.framework.common.pojo.PageResult;
import com.sugar.real.estate.framework.common.util.object.BeanUtils;
import com.sugar.real.estate.framework.excel.core.util.ExcelUtils;
import com.sugar.real.estate.module.business.controller.admin.infocls.vo.InfoClsPageReqVO;
import com.sugar.real.estate.module.business.controller.admin.infocls.vo.InfoClsRespVO;
import com.sugar.real.estate.module.business.controller.admin.infocls.vo.InfoClsSaveReqVO;
import com.sugar.real.estate.module.business.controller.admin.infocls.vo.InfoClsSimpleRespVO;
import com.sugar.real.estate.module.business.dal.dataobject.infocls.InfoClsDO;
import com.sugar.real.estate.module.business.service.infocls.InfoClsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.sugar.real.estate.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.sugar.real.estate.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 资讯分类")
@RestController
@RequestMapping("/business/info-cls")
@Validated
public class InfoClsController {

    @Resource
    private InfoClsService infoClsService;

    @PostMapping("/create")
    @Operation(summary = "创建资讯分类")
    @PreAuthorize("@ss.hasPermission('business:info-cls:create')")
    public CommonResult<Long> createInfoCls(@Valid @RequestBody InfoClsSaveReqVO createReqVO) {
        return success(infoClsService.createInfoCls(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资讯分类")
    @PreAuthorize("@ss.hasPermission('business:info-cls:update')")
    public CommonResult<Boolean> updateInfoCls(@Valid @RequestBody InfoClsSaveReqVO updateReqVO) {
        infoClsService.updateInfoCls(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资讯分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:info-cls:delete')")
    public CommonResult<Boolean> deleteInfoCls(@RequestParam("id") Long id) {
        infoClsService.deleteInfoCls(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资讯分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:info-cls:query')")
    public CommonResult<InfoClsRespVO> getInfoCls(@RequestParam("id") Long id) {
        InfoClsDO infoCls = infoClsService.getInfoCls(id);
        return success(BeanUtils.toBean(infoCls, InfoClsRespVO.class));
    }

    @GetMapping("/all")
    @Operation(summary = "获得所有资讯分类")
    @PreAuthorize("@ss.hasPermission('business:info-cls:query')")
    public CommonResult<List<InfoClsSimpleRespVO>> getInfoClsAll() {
        return success(infoClsService.getInfoClsAll());
    }

    @GetMapping("/page")
    @Operation(summary = "获得资讯分类分页")
    @PreAuthorize("@ss.hasPermission('business:info-cls:query')")
    public CommonResult<PageResult<InfoClsRespVO>> getInfoClsPage(@Valid InfoClsPageReqVO pageReqVO) {
        PageResult<InfoClsDO> pageResult = infoClsService.getInfoClsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InfoClsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资讯分类 Excel")
    @PreAuthorize("@ss.hasPermission('business:info-cls:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInfoClsExcel(@Valid InfoClsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InfoClsDO> list = infoClsService.getInfoClsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资讯分类.xls", "数据", InfoClsRespVO.class,
                        BeanUtils.toBean(list, InfoClsRespVO.class));
    }

}